<?php
class Accomodation extends Controller {
    private $imgdata = array(
        'dir' => array(
        'original' => 'images/uploads/original/',
        'thumb' => 'images/uploads/thumb/',
        'large'=>'images/uploads/large/'
    ),
    'total' => 0,
    'images' => array(),
    'error' => ''
    );
    
    public function accomodationlist() {
        $data["partnerlang"]=$this->config->item("partnerlang");
        $this->load->model('accomodation_model');
        $this->load->library("pagination");
        $config = array();
        $config["base_url"] = base_url() ."secu/accomodation/accomodationlist/";
        $config["total_rows"] = $this->accomodation_model->get_accomodation_list_count();
        $config["per_page"] = 10;
        $config["uri_segment"] = 4;
        $this->pagination->initialize($config);
        $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
        $data["pagination"] = $this->pagination->create_links();
        $acclist=$this->accomodation_model->get_accomodation_list($config["per_page"], $page);
        $data["acclist"]=$acclist;
        $this->template->set('controller', $this);
        $this->template->set('title', "login");
        $this->template->load_partial('admintemplate','accomodation/accomodationlist',$data);

    }

   public function addaccomodation() {
        $data["partnerlang"]=$this->config->item("partnerlang");
       // $this->template->load('admintemplate','admin/accomodation',$data);
        $this->template->set('controller', $this);
        $this->template->set('title', "login");
        $this->template->load_partial('admintemplate','accomodation/accomodation',$data);
    }

    public function doaddaccomodation()
    {
          $validate_accomodation = array(
                array( 'field' => 'txttitle',
                       'label' => 'Title',
                       'rules' => 'required')
            );
        $acomdationdetails=array();
             $acomdationdetails = array( 'acomdationdetails' => array(
             "txttitle"  =>$this->input->post('txttitle')
             ,"txtStatus"=>$this->input->post('txtStatus')
                     ));
          //   print_r($_POST);exit;
            $this->session->set_userdata($acomdationdetails);
            $this->form_validation->set_error_delimiters('<div class="alert">', '</div>');
            $this->form_validation->set_rules($validate_accomodation);

            if ($this->form_validation->run() == FALSE) {
                $this->template->set('controller', $this);
                $this->template->set('title', "login");
                $this->template->load_partial('admintemplate', 'accomodation/accomodation',$data);

            } else  {
              //$this->template->set('product_header', 'Admin Home');
              $this->load->model('accomodation_model');
              $this->accomodation_model->addnewaccomodation($acomdationdetails["acomdationdetails"]);
              redirect('secu/accomodation/accomodationlist');
              //$this->load->model('user_security');
            }

    }

    /*public function accomodationlist() {
        $data["partnerlang"]=$this->config->item("partnerlang");
        $this->load->model('accomodation');
        $acclist=$this->accomodation->get_accomodation_list();
        $data["acclist"]=$acclist;
        $this->template->load('admintemplate','admin/accomodationlist',$data);
    }*/

    public function accomodationedit()
    {
        $accid= $this->uri->segment(5);
    //    echo "****".$accid;exit;
        $data["partnerlang"]=$this->config->item("partnerlang");
        $this->load->model('accomodation_model');
        $accdetails=$this->accomodation_model->get_accomodation_details($accid);
        $acomdationdetails=array();
             $acomdationdetails = array( 'acomdationdetails' => array(
             "txttitle"  =>$acomdationdetails["accdesc"]
             ,"txtStatus"=>$acomdationdetails["status"]
                     ));
             $this->session->set_userdata($acomdationdetails);
        $data["accdetails"]=$accdetails;
        $this->template->load('admintemplate','secu/accomodation/accedit',$data);
    }

    public function doeditacomdation()
    {
          $validate_accomodation = array(
                array( 'field' => 'txttitle',
                       'label' => 'Title',
                       'rules' => 'required')
            );
        $acomdationdetails=array();
             $acomdationdetails = array( 'acomdationdetails' => array(
             "id"=>$this->input->post('id'),
             "txttitle"  =>$this->input->post('txttitle')
             ,"txtStatus"=>$this->input->post('txtStatus')
                     ));
          //   print_r($_POST);exit;
            $this->session->set_userdata($acomdationdetails);
            $this->form_validation->set_error_delimiters('<div class="alert">', '</div>');
            $this->form_validation->set_rules($validate_accomodation);

            if ($this->form_validation->run() == FALSE) {
                $this->template->set('controller', $this);
                $this->template->set('title', "login");
                $this->template->load_partial('admintemplate','accomodation/accedit',$data);
             //   $this->template->load('admintemplate', 'admin/accomodation',$data);

            } else  {
              //$this->template->set('product_header', 'Admin Home');
              $this->load->model('accomodation_model');
              $this->accomodation_model->doeditacomdation($acomdationdetails["acomdationdetails"]);
              redirect('secu/accomodation/accomodationlist');
              //$this->load->model('user_security');
            }

    }

     public function accomodationdelete()
    {

              //$this->template->set('product_header', 'Admin Home');
               $accid= $this->uri->segment(5);
              $this->load->model('accomodation_model');
              $this->accomodation_model->deleteacomdation($accid);
              redirect('secu/accomodation/accomodationlist');
              //$this->load->model('user_security');
      }
      
    /*public function featuredaccomodationlist() {
        $data["partnerlang"]=$this->config->item("partnerlang");
        $this->load->model('accomodation_model');
        $featuredacclist=$this->accomodation_model->get_featuredaccomodation_list();
        $data["featuredacclist"]=$featuredacclist;
        $this->template->set('controller', $this);
        $this->template->set('title', "login");
        $this->template->load_partial('admintemplate','accomodation/accomodationlist',$data);

    }*/
    
    public function addfeaturedaccomodation() {
        $this->load->model('accomodation_model');
        $featured_acc_id=$this->accomodation_model->is_featured_accomodation_available();
        if($featured_acc_id){            
            redirect('secu/accomodation/featuredaccomodationedit/id/'.$featured_acc_id['id']);
        }
        $data["partnerlang"]=$this->config->item("partnerlang");
       // $this->template->load('admintemplate','admin/accomodation',$data);
        $this->template->set('controller', $this);
        $this->template->set('title', "login");
        $this->template->load_partial('admintemplate','accomodation/featured_accomodation',$data);
    }
   
    public function doaddfeaturedaccomodation()
    {
          $validate_accomodation = array(
                array( 'field' => 'title',
                       'label' => 'Title',
                       'rules' => 'required'),
                array( 'field' => 'desc',
                       'label' => 'Description',
                       'rules' => 'required'),
                array( 'field' => 'date_from',
                       'label' => 'Date From',
                       'rules' => 'required')
            );
        $featuredacomdationdetails=array();
             $featuredacomdationdetails = array( 'featuredacomdationdetails' => array(
                "title"  =>$this->input->post('title')
                ,"desc"  =>$this->input->post('desc')
                ,"date_from"  =>$this->input->post('date_from')
                ,"date_to"  =>$this->input->post('date_to')                
                ,"isactive"=>$this->input->post('isactive')
                ));
          //   print_r($_POST);exit;
            $this->session->set_userdata($featuredacomdationdetails);
            $this->form_validation->set_error_delimiters('<div class="alert">', '</div>');
            $this->form_validation->set_rules($validate_accomodation);

            if ($this->form_validation->run() == FALSE) {
                $this->template->set('controller', $this);
                $this->template->set('title', "login");
                $this->template->load_partial('admintemplate', 'accomodation/featured_accomodation',$data);

            } else  {
                ini_set('display_errors',1);
              //$this->template->set('product_header', 'Admin Home');              
              $this->load->model('accomodation_model');              
              /*echo "<pre>";              
              //print_r($this->accomodation_model);
              echo "</pre>";*/
              $featured_acc_id=$this->accomodation_model->addnewfeaturedaccomodation($featuredacomdationdetails["featuredacomdationdetails"]);
              //exit('trrr');
              redirect('secu/accomodation/featuredaccomodationedit/id/'.$featured_acc_id);
              //$this->load->model('user_security');
            }

    }
    
    public function featuredaccomodationedit()
    {
        $featuredaccid= $this->uri->segment(5);
        $del_image_status= $this->uri->segment(7);
        $del_image= $this->uri->segment(6);
        if($del_image){
            $data['image_deleted']="Image {$del_image} deleted {$del_image_status}";
        }
    ////    echo "****".$accid;exit;
        $data["partnerlang"]=$this->config->item("partnerlang");
        $this->load->model('accomodation_model');
        $accdetails=$this->accomodation_model->get_featured_accomodation_details($featuredaccid);
        //print_r($accdetails);
        $acomdationdetails=array();
             $acomdationdetails = array( 'acomdationdetails' => array(
                "title"  =>$acomdationdetails["title"]
                ,"desc"  =>$acomdationdetails["desc"]
                ,"date_from"  =>$acomdationdetails["date_from"]
                ,"date_to"  =>$acomdationdetails["date_to"]
                ,"isactive"=>$acomdationdetails["isactive"]
                     ));
             $this->session->set_userdata($acomdationdetails);
        $data["acomdationdetails"]=$accdetails;
        $this->template->load('admintemplate','secu/accomodation/featured_accomodation',$data);
    }
    
    public function doeditfeaturedacomdation()
    {
          $validate_accomodation = array(
                array( 'field' => 'title',
                       'label' => 'Title',
                       'rules' => 'required'),
                array( 'field' => 'desc',
                       'label' => 'Description',
                       'rules' => 'required'),
                array( 'field' => 'date_from',
                       'label' => 'Date From',
                       'rules' => 'required')
            );
          //exit();
        $acomdationdetails=array();
             $acomdationdetails = array( 'acomdationdetails' => array(
            "id"=>$this->input->post('id'),
            "title"  =>$this->input->post('title')
            ,"desc"  =>$this->input->post('desc')
            ,"date_from"  =>$this->input->post('date_from')
            ,"date_to"  =>$this->input->post('date_to')                
            ,"isactive"=>$this->input->post('isactive')
                     ));             
          //   print_r($_POST);exit;
            $this->session->set_userdata($acomdationdetails);
            $this->form_validation->set_error_delimiters('<div class="alert">', '</div>');
            $this->form_validation->set_rules($validate_accomodation);

            if ($this->form_validation->run() == FALSE) {
                $this->template->set('controller', $this);
                $this->template->set('title', "login");
                $this->template->load_partial('admintemplate','accomodation/featured_accomodation',$data);
             //   $this->template->load('admintemplate', 'admin/accomodation',$data);

            } else  {
              //$this->template->set('product_header', 'Admin Home');
              $this->load->model('accomodation_model');
              $this->accomodation_model->doeditfeaturedacomdation($acomdationdetails["acomdationdetails"]);
              redirect('secu/accomodation/addfeaturedaccomodation');
              //$this->load->model('user_security');
            }

    }
    
    /*public function featuredaccomodationdelete()
    {
              //$this->template->set('product_header', 'Admin Home');
              $featuredaccid= $this->uri->segment(5);
              $this->load->model('accomodation_model');
              $this->accomodation_model->deletefeaturedacomdation($featuredaccid);
              redirect('secu/accomodation/featuredaccomodationlist');
              //$this->load->model('user_security');
     }*/

     public function savefeaturedaccimages()
     {
        $upload_from= $this->uri->segment(4);
        //exit($upload_from);
        $id=$this->input->post("id");
        $c_upload['upload_path']    = $this->imgdata['dir']['original'];
        $c_upload['allowed_types']  = 'gif|jpg|png|jpeg|x-png';
        $c_upload['max_size']       = '10000';
        $c_upload['max_width']      = '10000';
        $c_upload['max_height']     = '5000';
        $c_upload['remove_spaces']  = TRUE;
        //print_r($c_upload);
        
        $this->load->library('upload', $c_upload);

        if ($this->upload->do_upload()) {
//
            $img = $this->upload->data();
            $new_image = $this->imgdata['dir']['original'].trim($id)."_".trim(time())."_".$img['file_name'];
            $this->img_resize($img, $new_image,$id);
            
            unlink($this->imgdata['dir']['original'].$img['file_name']);
            //exit($this->imgdata['dir']['original'].$img['file_name']);
            $this->load->model('content');
            //exit();
            $this->content->saveimagestodb($id,trim($id)."_".trim(time())."_".$img['file_name'],$upload_from);



        } else {
            $this->imgerror= $this->upload->display_errors();            
        }
        if($upload_from=='featuredacc'){
            redirect('secu/accomodation/featuredaccomodationedit/id/'.$id);
        }else{
            redirect('admin/hotelcontent/?id='.trim($id)."#img");
        }

    }
    
    public function img_resize($upload,$new_image,$id)
    {
        $this->load->library('image_lib');
        $newpath = $new_image;

        $config['image_library'] = 'gd2';
        $config['source_image']    = $upload['full_path'];
        $config['new_image'] = $newpath;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 595;
        $config['height']    = 320;

        $this->image_lib->initialize($config);

        if ( ! $this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }

        $newpath_thumb = $this->imgdata['dir']['thumb']."thumb_".trim($id)."_".trim(time())."_".$upload['file_name'];

        unset($config);
        $this->image_lib->clear();
         $this->make_thumb($upload['full_path'],$newpath_thumb);
    }
    
    public function make_thumb($tempimage,$path)
    {

        $config['image_library'] = 'gd2';
        $config['source_image']    = $tempimage;
        $config['new_image'] = $path;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 100;
        $config['height']    = 80;

        $this->image_lib->initialize($config);

        if ( ! $this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }
    }
    
    public function deletefeaturedaccimage()
    {
        $original_status=false;
        $thumb_status=false;
        $del_image_status="failed";
        $id=$this->uri->segment(7);
        $ori_img=$this->uri->segment(5); 
        $ext=  substr($ori_img, -3);        
        $ori_img=  substr($ori_img, 0, strlen($ori_img)-4).'.'.$ext;
        $this->load->model('content');
        $this->content->deleteimage($id, "featuredacc");
        //
        //echo $this->imgdata['dir']['original'].$ori_img.'<br>';
        if (file_exists($this->imgdata['dir']['original'].$ori_img)){
            unlink($this->imgdata['dir']['original'].$ori_img);
            $original_status=true;
        }
        //echo $this->imgdata['dir']['thumb'].'thumb_'.$ori_img;
        if (file_exists($this->imgdata['dir']['thumb'].'thumb_'.$ori_img)){
            unlink($this->imgdata['dir']['thumb'].'thumb_'.$ori_img);
            $thumb_status=true;
        }
        //exit();
        if ($original_status && $thumb_status){
            $del_image_status="successfully";
        }elseif ($original_status || $thumb_status) {
            $del_image_status="partially";
        }
        redirect('secu/accomodation/featuredaccomodationedit/id/'.$id.'/'.$ori_img.'/'.$del_image_status);
    }
}
?>
